package com.lin.xd.bkprocrastination;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.Context;
import android.support.v4.view.MarginLayoutParamsCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Welcom extends ActionBarActivity {

    final Pattern pMail = Pattern.compile("^[\\w&&[^_]]+@[\\w&&[^_]]+\\.com$");
    final Pattern pPass = Pattern.compile("[\\w&&[^_]]+");

    PopupWindow popupWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);

        Button button_register = (Button) findViewById(R.id.button_register);
        button_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.register_layout,null);
                EditText editText_email = (EditText) view.findViewById(R.id.register_email);
                EditText editText_password = (EditText) view.findViewById(R.id.register_password);
                EditText editText_password2 = (EditText) view.findViewById(R.id.register_password2);

                editText_password.setMinEms(6);
                editText_password.setMaxEms(18);
                editText_password2.setMinEms(6);
                editText_password2.setMaxEms(18);

                popupWindow = new PopupWindow(view, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);

                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                            popupWindow.dismiss();
                            popupWindow = null;
                        }
                        return false;
                    }
                });
                editText_email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if(!hasFocus) {
                            EditText text1 = (EditText)v;
                            checkEmail(text1);
                        }
                    }
                });

                editText_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            EditText text = (EditText)v;
                            checkPassWord(text);
                        }
                    }
                });

                editText_password2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            EditText text = (EditText)v;
                            reCheckPass(text);
                        }
                    }
                });

                Button button_cancel = (Button) view.findViewById(R.id.register_cancel);
                button_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        popupWindow = null;
                    }
                });

                popupWindow.showAtLocation(v, Gravity.CENTER,0,0);
            }
        });


        Button button = (Button) findViewById(R.id.login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 View view = getLayoutInflater().inflate(R.layout.activity_login,null);
                popupWindow  = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                EditText editText = (EditText)view.findViewById(R.id.editText);
                EditText editText1 = (EditText)view.findViewById(R.id.editText1);

                Button button_cancel = (Button) view.findViewById(R.id.login_cancel);

                editText1.setMinEms(6);
                editText1.setMaxEms(30);

                view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                            popupWindow.dismiss();
                            popupWindow = null;
                        }
                        return false;
                    }
                });

                editText1.setFilters(new InputFilter[] {
                        new InputFilter.LengthFilter(30)
                });

                editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            EditText text = (EditText)v;
                            checkPassWord(text);
                        }
                    }
                });

                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if(!hasFocus) {
                            EditText text1 = (EditText)v;
                            String s = text1.getText().toString();
                            if (s.equals("")) {
                                Toast.makeText(Welcom.this,"账号不能为空",Toast.LENGTH_SHORT).show();
                            } else {
                                Matcher mMail = pMail.matcher(s);
                                if(!mMail.matches()) {
                                    text1.setTextColor(getResources().getColor(R.color.red));
                                    Toast.makeText(Welcom.this,"邮箱输入有误",Toast.LENGTH_SHORT).show();
                                } else {
                                    text1.setTextColor(getResources().getColor(R.color.black));
                                }
                            }
                        }
                    }
                });

                button_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                        popupWindow = null;
                    }
                });
                
                popupWindow.showAtLocation(v,Gravity.CENTER,0,0);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_welcom, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    //检查邮箱是否有误
    private void checkEmail (EditText text1){
        String s = text1.getText().toString();
        if (s.equals("")) {
            Toast.makeText(Welcom.this,"邮箱不能为空",Toast.LENGTH_SHORT).show();
        } else {
            Matcher mMail = pMail.matcher(s);
            if(!mMail.matches()) {
                text1.setTextColor(getResources().getColor(R.color.red));
                Toast.makeText(Welcom.this,"邮箱输入有误",Toast.LENGTH_SHORT).show();
            } else {
                text1.setTextColor(getResources().getColor(R.color.black));
            }
        }
    }

    //检查密码格式
    private void checkPassWord (EditText text) {
        String s = text.getText().toString();
        if(s.equals("")) {
            Toast.makeText(Welcom.this,"密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            if(s.length()<6) {
                Toast.makeText(Welcom.this,"密码至少为六位",Toast.LENGTH_SHORT).show();
            } else {
                Matcher matcher = pPass.matcher(s);
                if(!matcher.matches()) {
                    Toast.makeText(Welcom.this,"密码只能为数字或字母",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private void reCheckPass (EditText text) {
        String s = text.getText().toString();
        if(s.equals("")) {
            Toast.makeText(Welcom.this,"请确认密码", Toast.LENGTH_SHORT).show();
        } else {
            if(s.length()<6) {
                Toast.makeText(Welcom.this,"密码至少为六位",Toast.LENGTH_SHORT).show();
            } else {
                Matcher matcher = pPass.matcher(s);
                if(!matcher.matches()) {
                    Toast.makeText(Welcom.this,"密码只能为数字或字母",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

}
